{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy  as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "docs = {\n",
    "    'data':[ ['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'], \n",
    "                    ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park','stupid'],\n",
    "                   ['my', 'dalmation', 'is','so', 'cute', 'I', 'love', 'him'],\n",
    "                   ['stop', 'posting','stupid', 'worthless', 'garbage'],\n",
    "                   ['mr', 'licks', 'ate','my','steak', 'how', 'to','stop', 'him'],\n",
    "                   ['quit', 'buying', 'worthless', 'dog', 'food','stupid']],\n",
    " \n",
    "    'labels': [0,1,0,1,0,1]\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def docSetList(docs):\n",
    "    docSet = set([])\n",
    "    for doc in docs:\n",
    "        docSet = docSet | set(doc)\n",
    "\n",
    "    docList = list(docSet)\n",
    "    docList.sort()\n",
    "    return docList\n",
    "\n",
    "docList = docSetList(docs['data'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def doc2vector(docs, docList):\n",
    "    vector = [0] * len(docList)\n",
    "    for word in docs:\n",
    "        i = docList.index(word)\n",
    "        vector[i] +=1\n",
    "    return vector\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0],\n",
       " [0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  1,\n",
       "  0],\n",
       " [1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0],\n",
       " [0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1],\n",
       " [0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0],\n",
       " [0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  0,\n",
       "  1,\n",
       "  0,\n",
       "  0,\n",
       "  1]]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docsvector = list(map(lambda docs: doc2vector(docs, docList), docs['data']))\n",
    "docsvector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def trainNB0(trainMatrix,trainCategory):\n",
    "    numExamples = len(trainMatrix)\n",
    "    numWords = len(trainMatrix[0])\n",
    "    p0 = sum(trainCategory) / float(numExamples)\n",
    "    p0Num = np.ones(numWords); p1Num = np.ones(numWords) \n",
    "    p0Denom = 2.0; p1Denom = 2.0    \n",
    "    for i in range(numExamples):\n",
    "        if (int(trainCategory[i]) == 1):\n",
    "            p1Num +=trainMatrix[i]\n",
    "            p1Denom += sum(trainMatrix[i])\n",
    "        else:\n",
    "            p0Num +=  trainMatrix[i]\n",
    "            p0Denom += sum(trainMatrix[i] )\n",
    "    p1v = np.log(p1Num / p1Denom)\n",
    "    p0v = np.log(p0Num / p0Denom)\n",
    "    return p0v,p1v,p0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([-2.56494936, -2.56494936, -3.25809654, -2.56494936, -2.56494936,\n",
       "        -2.56494936, -2.56494936, -3.25809654, -3.25809654, -2.56494936,\n",
       "        -2.56494936, -2.15948425, -2.56494936, -2.56494936, -2.56494936,\n",
       "        -2.56494936, -3.25809654, -2.56494936, -1.87180218, -3.25809654,\n",
       "        -3.25809654, -2.56494936, -3.25809654, -2.56494936, -3.25809654,\n",
       "        -2.56494936, -2.56494936, -2.56494936, -3.25809654, -3.25809654,\n",
       "        -2.56494936, -3.25809654]),\n",
       " array([-3.04452244, -3.04452244, -2.35137526, -3.04452244, -3.04452244,\n",
       "        -1.94591015, -3.04452244, -2.35137526, -2.35137526, -3.04452244,\n",
       "        -3.04452244, -2.35137526, -3.04452244, -3.04452244, -3.04452244,\n",
       "        -3.04452244, -2.35137526, -3.04452244, -3.04452244, -2.35137526,\n",
       "        -2.35137526, -3.04452244, -2.35137526, -3.04452244, -2.35137526,\n",
       "        -3.04452244, -3.04452244, -2.35137526, -1.65822808, -2.35137526,\n",
       "        -2.35137526, -1.94591015]),\n",
       " 0.5)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p0v,p1v,p0=trainNB0(docsvector,docs['labels'])\n",
    "\n",
    "p0v,p1v,p0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d44d76ef8cbbc4331cecfe2e59228ac31ebb71026289858a116838be7168b60b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
